<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 模型基类
 *
 * @author      Eden
 * @datetime    2016/11/26 16:10
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class API_Model extends CI_Model
{

    // 数据
    protected $data = array();

    // 错误提示
    private $error = '';

    /**
     * 继承基类方法
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 设置错误消息
     * @param string $error
     */
    protected function setError($error = '')
    {
        $this->error = $error;
    }

    /**
     * 获取错误消息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 检查用户是否存在
     * @param $userId
     * @return mixed
     */
    protected function _checkUserExists($userId)
    {
        $uDb = clone($this->db);
        $query = $uDb->get_where('user', array('id' => $userId, 'status' => 1));
        return $query->row_array();
    }

    /**
     * 帖子是否存在
     * @param $bbsId
     * @return mixed
     */
    protected function _checkBbsExists($bbsId)
    {
        $bbsDb = clone($this->db);
        $query = $bbsDb->get_where('bbs', array('id' => $bbsId, 'status' => 1));
        return $query->row_array();
    }

    /**
     * 评论是否存在
     * @param $commentId
     * @return mixed
     */
    protected function _checkCommentExists($commentId)
    {
        $cDb = clone($this->db);
        $query = $cDb->get_where('bbs_comments', array('id' => $commentId, 'status' => 1));
        return $query->row_array();
    }

    /**
     * 获取配置
     * @param $key
     * @param string $field
     * @return bool
     */
    protected function _getConfigByKey($key, $field = '*')
    {
        if (empty($key)) {
            return false;
        }

        $query = $this->db->select($field)
            ->from('setting')
            ->where(array('use' => 'mobile', 'key' => $key))
            ->get();
        return $query->row_array();
    }

    /**
     * 基地是否存在
     * @param int $baseId
     * @return bool
     */
    protected function _checkBaseExists($baseId = 0)
    {
        if (empty($baseId)) {
            return false;
        }

        $baseDb = clone($this->db);
        $q = $baseDb->get_where('base', array('baseId' => $baseId));
        return $q->row_array();
    }

    /**
     * 更新PV
     * @param string $table
     * @param array $where
     * @param int $pv
     * @return bool
     */
    protected function addPv($table = '', $where = array(), $pv = 0)
    {
        if (empty($pv)) {
            $rowDb = clone($this->db);
            $q = $rowDb->get_where($table, $where);
            $row = $q->row_array();
            if (empty($row)) {
                return false;
            }
            $pv = intval($row['pv']);
        }

        $db = clone($this->db);
        $db->where($where);
        $db->update($table, array('pv' => $pv + 1));
    }

    /**
     * 直播
     * @param int $streamId
     * @return mixed
     */
    protected function _getStreamUrl($streamId = 0)
    {
        $db = clone($this->db);
        $q = $db->select('videoUrlHD, videoUrlSD, status')->get_where('stream_device', array('id' => $streamId));
        return $q->row_array();
    }


    /**
     * 获取被收藏的商品
     * @param int $userId
     * @param array $goodsIds
     * @return array
     */
    protected function _getCollects($userId = 0, $goodsIds = array())
    {
        $db = clone($this->db);
        $q = $db->select('goodsId')
            ->where('userId', $userId)
            ->where_in('goodsId', $goodsIds)
            ->from('user_collect')
            ->get();
        $goods = $q->result_array();
        $return = array();
        if (!empty($goods)) foreach($goods as $good){
            $return[] = $good['goodsId'];
        }
        return $return;
    }

    /**
     * 获取被添加到购物车的商品
     * @param int $userId
     * @param array $goodsIds
     * @return array
     */
    protected function _getCars($userId = 0, $goodsIds = array())
    {
        $db = clone($this->db);
        $q = $db->select('goodsId')
            ->where('userId', $userId)
            ->where_in('goodsId', $goodsIds)
            ->from('user_car')
            ->get();
        $goods = $q->result_array();
        $return = array();
        if (!empty($goods)) foreach($goods as $good){
            $return[] = $good['goodsId'];
        }
        return $return;
    }

    protected function _getNameAndAvatar($userId = 0)
    {
        $db = clone($this->db);
        $q = $db->select('u.id as userId, u.nickName, up.avatar')
            ->from('user as u')
            ->join('user_profile as up', 'up.userId = u.id')
            ->where('u.id', $userId)
            ->get();
        $row = $q->row_array();
        if (empty($row)) {
            $this->output->send(array(
                'state' => false,
                'error' => '用户不存在'
            ));
        }

        return array(
            'userId' => intval($row['userId']),
            'nickName' => value($row, 'nickName'),
            'avatar' => value($row, 'avatar')
        );
    }

    /**
     * 计算宽高
     * @param int $width
     * @param int $height
     * @param string $type
     * @return array
     */
    protected function countWidthHeight($width = 0, $height = 0, $type = 'ios')
    {
        if (empty($width) || empty($height)) {
            return [
                'width' => $this->width,
                'height' => $this->height
            ];
        } else {
            // 以宽为准备
            $scale = $this->width / $width;
            $width = $scale > 1 ? $width : $this->width;
            $height = $scale > 1 ? $height : ceil($height * $scale);

            return [
                'width' => intval($width),
                'height' => intval($height)
            ];
        }
    }
}
